package com.badibadi.activity;

import java.util.HashMap;
import java.util.Map;

import com.badibadi.mytools.CommonUtils;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.Convert;
import com.badibadi.mytools.RegexUtils;
import com.badibadi.mytools.Utils;
import com.badibadi.uniclubber.R;
import com.badibadi.uniclubber.R.layout;
import com.badibadi.uniclubber.R.menu;
import com.view.my_view.ValidateImageView;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
/**
 * 忘记密码第一步,修改日期 2014-10-8
 * @author Administrator
 *
 */
public class ForgotPassWordActivity extends BaseActivity implements OnClickListener{//ForgotPassWordSend

	//随机数（用于验证码）
	private String []number={
			"1","2","3","4","5","6","7","8","9","0"	
		};
	private String yanzhengma;//保存验证码
	private ValidateImageView validateImageView;//验证码图
	private EditText et_userName,et_yanzhengma;//两个文本框
	private TextView tv_cuowuyouxiang,tv_cuowuyanzhengma;//错误邮箱和错误验证码的提示
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
//			case 1://显示错误邮箱，不显示错误验证码
//				
//				
//				break;
//			case 2://不显示错误邮箱，显示错误验证码
//				
//				break;
//			case 3://两种都显示
//				
//				break;
//			case 4://两种都不显示
//				break;
//				
			case 5:
				Utils.ExitPrgress(ForgotPassWordActivity.this);
				try {
					Utils.showMessage(ForgotPassWordActivity.this, getResources().getString(R.string.l_net_error));
				} catch (Exception e) {
					// TODO: handle exception
				}
			
				break;
			case 6:
				Utils.ExitPrgress(ForgotPassWordActivity.this);
				try {
					Utils.showMessage(ForgotPassWordActivity.this, getResources().getString(R.string.SystemError));
				} catch (Exception e) {
					// TODO: handle exception
				}
				break;
			case 7://成功
				Utils.ExitPrgress(ForgotPassWordActivity.this);
				try {
					Intent intent =new Intent(ForgotPassWordActivity.this,ForgotPassWord2Activity.class);
				intent.putExtra("email", et_userName.getText().toString().trim());
					startActivity(intent);
				} catch (Exception e) {
					// TODO: handle exception
				}
			     
				break;
			case 8:
				Utils.ExitPrgress(ForgotPassWordActivity.this);
				try {
					Utils.showMessage(ForgotPassWordActivity.this, getResources().getString(R.string.TheOperationFailure));
				} catch (Exception e) {
					// TODO: handle exception
				}
				break;
			default:
				break;
			}
		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		goToTheTitle();
		setContentView(R.layout.activity_forgot_pass_word);
		init();
	}

	private void init() {
		// TODO Auto-generated method stub
		//发送
	  this.findViewById(R.id.ForgotPassWordSend).setOnClickListener(this);
	  tv_cuowuyouxiang=(TextView)this.findViewById(R.id.cuowuyouxiang);
	  tv_cuowuyanzhengma=(TextView)this.findViewById(R.id.cuowuyanzhengma);
	  validateImageView=(ValidateImageView)findViewById(R.id.validateImageView1);
	  et_yanzhengma=(EditText)findViewById(R.id.et_yanzhengma);
	  et_userName=(EditText)findViewById(R.id.et_userName);
	  //获取验证码
	  yanzhengma=getValidataText(validateImageView, number);
//	     
	  this.findViewById(R.id.huanyizhang).setOnClickListener(this);
	  this.findViewById(R.id.return_btn).setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId())
		{
		case R.id.ForgotPassWordSend://发送
			send();
			break;
		case R.id.huanyizhang:
//			换验证码的图
			gengxin();
			break;
		case R.id.return_btn:
			finish();
			break;
		}
	}
	
	
	public void send()
	{
		//邮箱
		if(RegexUtils.checkEmail(et_userName.getText().toString().trim()))
		{
			if(startValidata(et_yanzhengma)==true)
			{
//		     Intent intent=new Intent(ForgotPassWordActivity.this,ForgotPassWord2Activity.class);
//			 startActivity(intent);
//			 overridePendingTransition(R.anim.slide_left, R.anim.slide_right);
				
				forgetpwd();
				
			}
			else
			{
				//重新输入
				gengxin();
				gengxinTishi(false, true);
			}
		}
		else
		{
              gengxinTishi(true, false);	
		}
		
		
	}
	/**
	 * 用户找回密码
	 */
	private void forgetpwd()
	{
		Utils.showPrgress(ForgotPassWordActivity.this);
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("email",et_userName.getText().toString().trim());
		CommonUtils.CommomPostfunction(ForgotPassWordActivity.this, Constants.BadiUrl_1+"user/forgetpwd", map, handler, 5, 6, 7, 8);
		
		
	}
	
	
	/**
	 * 更新提示信息
	 * @param youxiang_isShow
	 * @param yanzhengma_isShow
	 * @return
	 */
	public void gengxinTishi(boolean youxiang_isShow,boolean yanzhengma_isShow)
	{
		if(youxiang_isShow==true)
		{
			tv_cuowuyouxiang.setVisibility(View.VISIBLE);
//			 tv_cuowuyouxiang=(TextView)this.findViewById(R.id.cuowuyouxiang);
//			  tv_cuowuyanzhengma=(TextView)this.findViewById(R.id.cuowuyanzhengma);
		}
		else
		{
			tv_cuowuyouxiang.setVisibility(View.INVISIBLE);
		}
		if(yanzhengma_isShow==true)
		{
			tv_cuowuyanzhengma.setVisibility(View.VISIBLE);
//			 tv_cuowuyouxiang=(TextView)this.findViewById(R.id.cuowuyouxiang);
//			  tv_cuowuyanzhengma=(TextView)this.findViewById(R.id.cuowuyanzhengma);
		}
		else
		{
			tv_cuowuyanzhengma.setVisibility(View.INVISIBLE);
		}
	}
	
	/**
	 * 判断验证码是否一致
	 * @param et 文本框
	 */
	public boolean startValidata(EditText et)
	{
	    if(et.getText().toString().trim().equals(yanzhengma))
	    {
	    	return true;
	    }
	    else
	    {
	    	return false;
	    }
	}
	/**
	 * 更新验证码
	 */
	public void gengxin()
	{
		  yanzhengma=getValidataText(validateImageView, number);
	}
	/**
	 * 获取验证码
	 * @return
	 */
	public String getValidataText(ValidateImageView img,String[] num)
	{
		StringBuffer buff=new StringBuffer("");
		String[] c=img.getValidataAndSetImage(num);
		for(int i=0;i<c.length;i++)
		{
			buff.append(c[i]);
		}
		String yzm=buff.substring(0, buff.length());
		return yzm;
	}
}
